package dongze.com.ckdj.ui;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.BindingMethod;
import android.databinding.BindingMethods;
import android.databinding.DataBindingUtil;
import android.databinding.InverseBindingListener;
import android.databinding.InverseBindingMethod;
import android.databinding.InverseBindingMethods;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import dongze.com.ckdj.R;
import dongze.com.ckdj.databinding.MassLossPictureItemBinding;
import dongze.com.ckdj.databinding.model.MassLossImageItemModel;
import dongze.com.ckdj.util.MyLog;

/**
 * Created by fanqwz on 2017/7/24.
 */


@BindingMethods({@BindingMethod(type = MassLossPictureGridLayout.class,
        attribute = "images", method = "setPictureList")})
@InverseBindingMethods({@InverseBindingMethod(attribute = "images", type = MassLossPictureGridLayout.class, event = "pictureListChanged", method = "getPictureList")})
public class MassLossPictureGridLayout extends GridLayout {
    private final static String TAG = MassLossPictureGridLayout.class.getSimpleName();
    private List<Bitmap> mPictures;
    private OnListChangedListener mOnPictureListChangedListener;

    public MassLossPictureGridLayout(Context context) {
        super(context);
    }

    public MassLossPictureGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MassLossPictureGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setPictureList(final List<Bitmap> pictures) {
        MyLog.d(TAG, "setPictureList: ");
        this.mPictures = pictures;
    }

    public List<Bitmap> getPictureList() {
        MyLog.d(TAG, "getPictureList: ");
        return mPictures;
    }

    public void setListener(OnListChangedListener listener) {
        mOnPictureListChangedListener = listener;
    }

    public interface OnListChangedListener {
        void onListChanged();
    }

    @BindingAdapter("pictureListChanged")
    public static void setListChangedListener(MassLossPictureGridLayout massLossPictureGridLayout, final InverseBindingListener inverseBindingListener) {
        if (inverseBindingListener == null) {
            massLossPictureGridLayout.setListener(null);
        } else {
            massLossPictureGridLayout.setListener(new OnListChangedListener() {
                @Override
                public void onListChanged() {
                    inverseBindingListener.onChange();
                }
            });
        }
    }
}
